/**
 * @(#)User.java, 十月 28, 2016.
 * <p>
 * Copyright 2016 fenbi.com. All rights reserved.
 * FENBI.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.wasp.collector.data;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;

import com.google.common.collect.Maps;
import com.wasp.collector.service.DefaultRetryHandler;
import com.wasp.collector.service.HttpClientCollector;
import com.wasp.collector.service.IRetryHandler;
import com.wasp.collector.utils.HttpConstant;

public class RequestWrapper implements Serializable{
	
	private static final long serialVersionUID = -8253698621080318292L;
	//private List<RequestContext> contexts;
	private String method;
	private String domain;
	private Map<String, String> cookies = Maps.newHashMap();
	private Map<String, String> header = Maps.newHashMap();
	private RequestConfig requestConfig = null;
	private String contentType = HttpConstant.ContentType.HTML;
	private String charSet = HttpConstant.Enconding.UTF8;
	private byte[] body;
	private int maxRetry = 3;
	private int errorSleepTime = 0;
	private IRetryHandler retryHandler = null;
	private String uId;
	
	private RequestWrapper(){
		uId = UUID.randomUUID().toString();
				
	}
	
	public byte[] getBody(){
		return body;
	}
	public RequestWrapper setBody(byte[] body){
		this.body = body;
		return this;
	}
	
	/*public List<RequestContext> getContexts() {
		return contexts;
	}

	public RequestWrapper setContexts(List<RequestContext> contexts) {
		this.contexts = contexts;
		return this;
	}
*/
	public RequestWrapper setGet() {
		method = HttpConstant.Method.GET;
		return this;
	}

	public RequestWrapper setPost() {
		method = HttpConstant.Method.POST;
		return this;
	}

	public String getMethod() {
		return method;
	}

	public RequestWrapper setDomain(String name) {
		this.domain = name;
		return this;
	}

	public RequestWrapper setCookie(String name, String value) {
		cookies.put(name, value);
		return this;
	}

	public RequestWrapper setHeader(String name, String value) {
		header.put(name, value);
		return this;
	}

	public Map<String, String> getCookies() {
		return this.cookies;
	}

	public Map<String, String> getHeader() {
		return this.header;
	}

	public String getDomain() {
		return this.domain;
	}

	public static RequestWrapper create() {
		RequestWrapper request = new RequestWrapper();
		request.setHeader(HttpConstant.Header.USER_AGENT, 
				HttpConstant.DEFAULTUSERAGENT);
		return request;
	}

	public RequestConfig getRequestConfig() {
		return requestConfig;
	}

	public RequestWrapper setRequestConfig(RequestConfig requestConfig) {
		this.requestConfig = requestConfig;
		return this;
	}
	public RequestWrapper setCharset(String charset){
		this.charSet = charset;
		return this;
	}
	public String getCharset(){
		return charSet;
	}
	public String getContentType() {
		return contentType;
	}
	public RequestWrapper setContentType(String contentType) {
		this.contentType = contentType;
		return this;
	}
	public RequestWrapper setRetryHandler(int errorSleepTime,
			int maxRetry,
			IRetryHandler handler){
		this.errorSleepTime = errorSleepTime;
		this.maxRetry = maxRetry;
		this.retryHandler = handler;
		return this;
	}
	public int getErrorSleepTime(){
		return errorSleepTime;
	}
	public int getMaxRetry(){
		return maxRetry;
	}
	public IRetryHandler getRetryHandler(){
		return retryHandler;
	}
	public String toString(){
		return uId;
	}
	public String getId(){
		return uId;
	}
}